package org.swidgets.sTabPanel;

import java.util.Vector;

import org.swidgets.base.spanels.SSimplePanel;
import org.swidgets.base.spanels.TabWidget;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

public class STabPanel extends Composite{
	private TabPanel tabPanel;
	private SSimplePanel simplePanel;
	private Vector tabs = new Vector();
		
	public STabPanel(){		
		super();
				
		simplePanel = new SSimplePanel();	
		
		tabPanel = new TabPanel();
		tabPanel.setWidth("100%");

		this.initWidget(simplePanel);
		simplePanel.add(tabPanel);
		
		tabPanel.addTabListener(new TabListener(){
			public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
				return true;
			}

			public void onTabSelected(SourcesTabEvents sender, int tabIndex) {	
				for (int i=0; i<tabs.size(); i++){
					((TabWidget) tabs.get(i)).unselect();
				}
				
				TabWidget selectedTab = (TabWidget) tabs.get(tabIndex);
				selectedTab.select();							
			}			
		});
	}
		
	public void add(Widget w, String tabText) {
		TabWidget tab = new TabWidget(tabText);
		SSimplePanel panel = new SSimplePanel();
		panel.setWidth("100%");
		panel.setHeight("100%");
		panel.add(w);
		
		tabPanel.add(panel, tab);
		tabs.add(tab);
	}
	
	public void add(Widget w, Widget tabWidget) {
		tabPanel.add(w, tabWidget);
	}

	public void addTabListener(TabListener listener) {
		tabPanel.addTabListener(listener);
	}

	public DeckPanel getDeckPanel() {
		return tabPanel.getDeckPanel();
	}

	public TabBar getTabBar() {
		return tabPanel.getTabBar();
	}

	public Widget getWidget(int index) {
		return tabPanel.getWidget(index);
	}

	public int getWidgetIndex(Widget widget) {
		return tabPanel.getWidgetIndex(widget);
	}

	public void insert(Widget widget, String tabText, boolean asHTML,
			int beforeIndex) {
		tabPanel.insert(widget, tabText, asHTML, beforeIndex);
	}

	public void insert(Widget widget, String tabText, int beforeIndex) {
		tabPanel.insert(widget, tabText, beforeIndex);
	}

	public void insert(Widget widget, Widget tabWidget, int beforeIndex) {
		tabPanel.insert(widget, tabWidget, beforeIndex);
	}

	public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
		return tabPanel.onBeforeTabSelected(sender, tabIndex);
	}

	public void onBrowserEvent(Event event) {
		tabPanel.onBrowserEvent(event);
	}

	public boolean remove(int index) {
		return tabPanel.remove(index);
	}

	public boolean remove(Widget widget) {
		return tabPanel.remove(widget);
	}

	public void removeTabListener(TabListener listener) {
		tabPanel.removeTabListener(listener);
	}

	public void selectTab(int index) {
		tabPanel.selectTab(index);
	}

	public void setTitle(String title) {
		tabPanel.setTitle(title);
	}
}
